package top.llove.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Druid配置
 * @author: 幸会
 * @date: 2021/6/4 11:34
 * @email: 1846724873@qq.com
 * @description: DruidConfig
 * version: 1.0
 */
@Configuration
public class DruidConfig {
    private final Logger LOG = LoggerFactory.getLogger(DruidConfig.class);
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DruidDataSource druidDataSource() {
        return new DruidDataSource();
    }

    /**
     * 配置监控服务器
     * @return 返回监控注册的servlet对象
     */
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean registry = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

        // 添加白名单
        registry.addInitParameter("all", "");
        // 添加黑名单
        registry.addInitParameter("deny", "192.168.31.1");
        // 添加控制台管理用户
        registry.addInitParameter("loginUsername", "druid");
        registry.addInitParameter("loginPassword", "druid");

        // 是否可以充值数据
        registry.addInitParameter("resetEnable", "false");

        LOG.info("Druid 加载成功");

        return registry;
    }


    /**
     * 配置服务过滤起
     * @return 返回过滤器配置对象
     */
    @Bean
    public FilterRegistrationBean statFilter() {
        FilterRegistrationBean filter = new FilterRegistrationBean(new WebStatFilter());
        // 添加过滤规则
        filter.addUrlPatterns("/*");
        // 忽略过滤格式
        filter.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");


        return filter;

    }











}
